package com.hly.musicbackground.model.auth.service.impl;

import com.hly.musicbackground.Enum.RedisKeyEnum;
import com.hly.musicbackground.Utils.MailUtils;
import com.hly.musicbackground.Utils.StringUtils;
import com.hly.musicbackground.exception.BusinessException;
import com.hly.musicbackground.model.auth.service.VerificationMailService;
import com.hly.musicbackground.model.user.service.UserService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VerificationMailServiceImpl implements VerificationMailService {

    private final MailUtils mailUtils;

    private final StringRedisTemplate redisTemplate;

    private final UserService userService;

    @Override
    public void sendMail(String mail, String type) {

        if (redisTemplate.hasKey(RedisKeyEnum.SEND_MAIL.getValue() + mail))
            Code.TOKEN_ERR("邮箱已发送，五分钟有效");

        String textMail = getRandomText(mail);
        String text = "";
        if (("password".equals(type) || "login".equals(type))){
            if (userService.getUserInfo(mail) == null){
                throw new BusinessException(Code.BUSINESS_ERR, "邮箱有误");
            }
            text = "【music】验证码" + textMail +
                    "。转发可能导致帐号被盗。如果这不是你本人操作，可在客户端反馈。";
        }else {
            if (userService.getUserInfo(mail) != null){
                throw new BusinessException(Code.BUSINESS_ERR, "邮箱也被注册");
            }
            text = "【music】你正在music注册账号，验证码" + textMail +
                    "。欢迎您的加入！";
        }
        redisTemplate.opsForValue().set(RedisKeyEnum.SEND_MAIL.getValue() + mail, textMail, 60 * 5, TimeUnit.SECONDS);
        mailUtils.sendHtmlMailMessage(mail ,"music", getTextTemplate(text));
    }


    public String getRandomText(String mail) {
        String str = "";
        Random random = new Random();
        for (int i = 0;i < 6;i++){
            str += random.nextInt(10);
        }
        return str;
    }

    @Override
    public void isVerificationMail(String mail, String sms) {
        String s = redisTemplate.opsForValue().get(RedisKeyEnum.SEND_MAIL.getValue() + mail);
        if (StringUtils.isEmpty(s)){
            throw new BusinessException(Code.BUSINESS_ERR, "验证码已过期");
        }
        if (!s.equals(sms)){
            throw new BusinessException(Code.BUSINESS_ERR, "验证码错误");
        }
    }


    public String getTextTemplate(String msg){
        return "<div style='border-radius: 25px;line-height: 30px;;padding: 10px 20px;display: flex;justify-content: center;align-items: center;background: rgb(35, 196, 110);height: 300px;color: #fff;'>" +
                "    <p>" + msg + "，邮件5分钟有效。</p>" +
                "  </div>";
    }

}
